package com.cuitrip.apiservice;

import com.alibaba.fastjson.TypeReference;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.business.bill.BillDetailFragment;
import com.cuitrip.business.bill.Bills;
import com.cuitrip.business.bill.CashDetailFragment;
import com.cuitrip.business.bill.Rate;
import com.cuitrip.business.comment.ReviewListItem;
import com.cuitrip.business.home.recommend.model.ListResponse;
import com.cuitrip.business.order.model.CancelReason;
import com.cuitrip.business.order.model.ConversationOrderItem;
import com.cuitrip.business.order.model.OrderItem;
import com.cuitrip.business.order.model.RefundInfo;
import com.cuitrip.business.pay.model.DiscountItem;
import com.cuitrip.business.pay.model.PayMode;
import com.cuitrip.business.user.model.CtUserInfo;
import com.lab.network.model.CtApiRequest;
import com.lab.network.model.CtRemoteBusiness;
import com.lab.network.model.ParseType;
import com.lab.network.model.ProxyResult;
import com.lab.network.model.ResultType;
import com.lab.network.proxy.ApiProxy;

public class OrderBusiness {

    public static void getOrderList(ApiProxy apiProxy,
                                    int userType, int start, int end, boolean isHistory) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_ORDER_LIST);
        ctApiRequest.addParam("userType", userType);
        ctApiRequest.addParam("start", start);
        ctApiRequest.addParam("size", end);
        if (isHistory) {
            ctApiRequest.addParam("lookHistoryOrder", 1);
        } else {
            ctApiRequest.addParam("lookHistoryOrder", 0);
        }
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ResultType.ARRAY, ConversationOrderItem.class));
    }

    public static ProxyResult getSyncOrderList(ApiProxy apiProxy,
                                           int userType, int start, int end, boolean isHistory) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_ORDER_LIST);
        ctApiRequest.addParam("userType", userType);
        ctApiRequest.addParam("start", start);
        ctApiRequest.addParam("size", end);
        if (isHistory) {
            ctApiRequest.addParam("lookHistoryOrder", 1);
        } else {
            ctApiRequest.addParam("lookHistoryOrder", 0);
        }
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        return apiProxy.postSync(ctRemoteBusiness, new ParseType(ResultType.ARRAY, ConversationOrderItem.class));
    }


    /**
     * 获取订单信息
     */
    public static void getOrderInfo(ApiProxy apiProxy,
                                    String oid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_ORDER_INFO);
        ctApiRequest.addParam("oid", oid);
        ctApiRequest.removeCurrencyParam();
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(OrderItem.class));
    }

    public static void getRefundInfo(ApiProxy apiProxy,
                                     String oid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_REFUND_INFO);
        ctApiRequest.addParam("orderId", oid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(RefundInfo.class));
    }

    /**
     * 旅行者取消旅程
     * @param oid
     * @return
     */
    public static void cancelOrder(ApiProxy apiProxy,
                                            String oid, String reason) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.CANCEL_ORDER);
        ctApiRequest.addParam("oid", oid);
        ctApiRequest.addParam("reason", reason);
        ctApiRequest.addParam("type", "1");
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }


    public static void refuseOrder(ApiProxy apiProxy,
                                            String oid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.CANCEL_ORDER);
        ctApiRequest.addParam("oid", oid);
        ctApiRequest.addParam("type", "2");
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    /**
     * 发现者确认预约
     * @param oid
     * @return
     */
    public static void confirmOrder(ApiProxy apiProxy,
                                             String oid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.CONFIRM_ORDER);
        ctApiRequest.addParam("oid", oid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    /**
     * 开始旅程
     * @param oid
     * @return
     */
    public static void beginOrder(ApiProxy apiProxy,
                                           String oid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.BEGIN_ORDER);
        ctApiRequest.addParam("oid", oid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    /**
     * 结束旅程，订单关闭
     * @param oid
     * @return
     */
    public static void endOrder(ApiProxy apiProxy,
                                         String oid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.END_ORDER);
        ctApiRequest.addParam("oid", oid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void submitReview(ApiProxy apiProxy,
                                             String oid, String serviceScore, String content) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.SUBMIT_COMMENT);
        ctApiRequest.addParam("oid", oid);
        ctApiRequest.addParam("serviceScore", serviceScore);
        ctApiRequest.addParam("content", content);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void submitNPS(ApiProxy apiProxy,
                                 int score, String type, String feedback) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.SUBMIT_NPS_INFO);
        ctApiRequest.addParam("type", type);
        ctApiRequest.addParam("score", score);
        ctApiRequest.addParam("feedback", feedback);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void getReviewList(ApiProxy apiProxy,
                                     String sid) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_REVIEW_LIST);
        ctApiRequest.addParam("sid", sid);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ResultType.ARRAY, ReviewListItem.class));
    }

    public static void createOrder(ApiProxy apiProxy,String insiderId, String sid, String serviceName,
                                   long serviceDate, String buyerNum, String servicePrice,
                                   String moneyType,String payCurrency){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.CREATE_ORDER);
        ctApiRequest.addParam("insiderId", insiderId);
        ctApiRequest.addParam("sid", sid);
        ctApiRequest.addParam("serviceName", serviceName);
        ctApiRequest.addParam("serviceDate", serviceDate);
        ctApiRequest.addParam("buyerNum", buyerNum);
        ctApiRequest.addParam("servicePrice", servicePrice);
        ctApiRequest.addParam("moneyType", moneyType);
        ctApiRequest.addParam("payCurrency", payCurrency);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(OrderItem.class));
    }

    /**
     * 支付订单
     */
    public static void payOrder(ApiProxy apiProxy, String orderId, String inviteCode) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.PAY_ORDER);
        ctApiRequest.addParam("orderId", orderId);
        ctApiRequest.addParam("inviteCode", inviteCode);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(OrderItem.class));
    }

    /**
     * 修改订单
     */
    public static void modifyOrder(ApiProxy apiProxy, String orderId, String sid, String serviceName,
                                   long serviceDate, String buyerNum, String servicePrice,
                                   String moneyType) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.MODIFY_ORDER);
        ctApiRequest.addParam("orderId", orderId);
        ctApiRequest.addParam("sid", sid);
        ctApiRequest.addParam("serviceName", serviceName);
        ctApiRequest.addParam("serviceDate", serviceDate);
        ctApiRequest.addParam("buyerNum", buyerNum);
        ctApiRequest.addParam("servicePrice", servicePrice);
        ctApiRequest.addParam("payCurrency", moneyType);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(OrderItem.class));
    }


    /**
     * 获取订单支付详情
     */
    public static void getCharge(ApiProxy apiProxy,String oid, String channel, String clientIp, String payCurrency,
                                 String couponId){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_CHARGE);
        ctApiRequest.addParam("orderId", oid);
        ctApiRequest.addParam("channel", channel);
        ctApiRequest.addParam("clientIp", clientIp);
        ctApiRequest.addParam("payCurrency", payCurrency);
        ctApiRequest.addParam("couponIds", couponId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void getFinalPrice(ApiProxy apiProxy,String oid,String payCurrency,String channel,String couponId){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.FINAL_PRICE);
        ctApiRequest.addParam("orderId", oid);
        ctApiRequest.addParam("payCurrency", payCurrency);
        ctApiRequest.addParam("channel",channel);
        ctApiRequest.addParam("couponIds", couponId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(PayMode.class));
    }

    public static void getBills(ApiProxy apiProxy, int start, int size) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_BILLS);
        ctApiRequest.addParam("start", start);
        ctApiRequest.addParam("size", size);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(Bills.class));
    }

    public static void getValidCoupon(ApiProxy apiProxy, String payCurrency) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.VALID_COUPON);
        ctApiRequest.addParam("payCurrency", payCurrency);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(new TypeReference<ListResponse<DiscountItem>>() {
        }.getType()));
    }

    public static void updateOrderConversation(ApiProxy apiProxy, String orderId, String targetId) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.UPDATE_TARGET_ID);
        ctApiRequest.addParam("orderId", orderId);
        ctApiRequest.addParam("targetId", targetId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }

    public static void getOrderByRongTargetId(ApiProxy apiProxy, String targetId, boolean isAsyncExecute) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_ORDER_INFO_BY_TARGET_ID);
        ctApiRequest.addParam("targetId", targetId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.setAsyncExecute(isAsyncExecute);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(OrderItem.class));
    }

    public static void getBillDetail(ApiProxy apiProxy, String billId) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.BILL_DETAIL);
        ctApiRequest.addParam("billId", billId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(BillDetailFragment.BillDetail.class));
    }

    public static void getCashDetail(ApiProxy apiProxy, String cashId) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.BILL_DETAIL);
        ctApiRequest.addParam("billId", cashId);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CashDetailFragment.CashDetail.class));
    }

    public static void getRates(ApiProxy apiProxy, int start, int size) {
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_RATES);
        ctApiRequest.addParam("start", start);
        ctApiRequest.addParam("size", size);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(ResultType.ARRAY,Rate.class));
    }

    public static void getCancelReason(ApiProxy apiProxy,String type){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.GET_CANCEL_REASON);
        ctApiRequest.addParam("type", type);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CancelReason.class));
    }
    public static void checkBasicUserInfo(ApiProxy apiProxy){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.CHECK_BASIC_USER_INFO);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType(CtUserInfo.class));
    }

    public static void submitCode(ApiProxy apiProxy,String code){
        CtApiRequest ctApiRequest = new CtApiRequest();
        ctApiRequest.setApiName(ApiManager.SUBMIT_CODE);
        ctApiRequest.addParam("code", code);
        CtRemoteBusiness ctRemoteBusiness = new CtRemoteBusiness(ctApiRequest);
        apiProxy.postAsync(ctRemoteBusiness, new ParseType());
    }
}
